package com.lemon.smartsafety.ui.activity.safe.data;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.event.datacoll.BuildingGroupRefreshEvent;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.SafeDataServices;
import com.lemon.smartsafety.ui.activity.common.SelectBaseActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import org.greenrobot.eventbus.Subscribe;

import butterknife.OnClick;

/**
 * Created by Cooliean on 16/4/6.
 */
public class BuildingGroupListActivity extends SelectBaseActivity {

    private String mCompanyCode = "";


    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        if (getIntent().getExtras() != null) {
            mCompanyCode = getIntent().getStringExtra(BundleKey.COMPANY_CODE);
        }
        mCompanyCode = StringUtils.nullValue(mCompanyCode);
    }

    @Override
    public String getTag() {
        return EventTag.BUILDING_GROUP_LIST;
    }

    @Override
    public void getNetData() {
        SafeDataServices.init().getBuildingGroupList(RefreshType.REFRESH, 1000, 1, "");
    }


    @Override
    protected void initTitle() {
        setBarTitle("建筑分组");
    }


    @Override
    protected void initViews() {
        super.initViews();
        if (mFab != null) {
            mFab.setVisibility(View.VISIBLE);
        }
    }


    @OnClick({R.id.float_button})
    public void floatButtonClick(View view) {
        BuildingGroupManagerActivity.showAddDialog(this, mHUDUtil, mCompanyCode,"","");
    }


    /**
     * http处理请求的处理
     *
     * @param event
     */
    @Override
    public void onEventMainThread(HttpCallbackEvent event) {
        super.onEventMainThread(event);
        if (EventTag.TAG_BUILDING_GROUP_ACTION.equals(event.getTag())) {
            if (event.getHttpFlow() == HandlerCallback.HttpFlow.START) {
                mHUDUtil.showHUD();
            } else if (event.getHttpFlow() == HandlerCallback.HttpFlow.FINISH) {
                if (event.getEventResult() == Constants.Result.SUCCESS) {
                    mHUDUtil.showDoneTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            mPullToRefreshRecyclerView.beginRefreshing();
                        }
                    });
                } else {
                    mHUDUtil.showErrorTip(event.getMsg());
                }
            }

        }
    }

    @Subscribe
    public void onEventMainThread(BuildingGroupRefreshEvent event) {
        getNetData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_manager, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_manager) {
            goGroupManager();
        }
        return super.onOptionsItemSelected(item);
    }


    private void goGroupManager() {
        Bundle bundle = new Bundle();
        bundle.putString(BundleKey.COMPANY_CODE, StringUtils.nullValue(mCompanyCode));
        UIHelper.showActivity(this, bundle, BuildingGroupManagerActivity.class);
    }
}
